package com.block.channel.flow.channel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channel.flow.IChannelReportService;
import com.block.channel.model.result.ReprotFlowResult;
import com.block.common.bean.CommonParam;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelBaseInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class BianXMChannelReportService implements IChannelReportService {

    private static Logger log = LogFactory.getCommLog();

    private String requestUrl = "https://openlog.bianxianmao.com/inads/s.gif";

    @Override
    public ReprotFlowResult registerActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        ReprotFlowResult flowResult = new ReprotFlowResult();
        flowResult.setSuccess(false);

        JSONObject headObject = commonParam.getHeadObject();
        String bxm_id = headObject.getString("bxm_id");
        if (StrUtil.isEmpty(bxm_id)){
            flowResult.setCode("-2");
            flowResult.setMessage("bxm_id参数为空，不做处理");
            return flowResult;
        }

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("mt", "8");
        bizMap.put("bxmid", bxm_id);
        bizMap.put("conversion_type", 2);

        log.info("【变现猫信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        HttpResponse httpResponse = HttpUtil.createGet(requestUrl)
                .form(bizMap)
                .execute();
        log.info("【变现猫信息流上报】【注册】【开始结束】【result】{}",  httpResponse.getStatus());

        Integer status = httpResponse.getStatus();
        String body = httpResponse.body();

        flowResult.setCode(String.valueOf(status));
        flowResult.setMessage(body);
        flowResult.setParamInfo(JSON.toJSONString(bizMap));
        flowResult.setResultInfo(status +";"+body);
        if (ObjectUtil.equal(status, Integer.valueOf(200))){
            // 上报不成功报异常
            flowResult.setSuccess(true);
        }else{
            flowResult.setSuccess(false);
        }
        return flowResult;
    }
    @Override
    public ReprotFlowResult sourceActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }
    @Override
    public ReprotFlowResult downloadActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }

    @Override
    public ReprotFlowResult payBuyActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }


}
